This code provides a utility function called streamToGoogle
that uploads files or streams to a specified Google Cloud Storage bucket, handling bucket creation and metadata.
npm run import -- "upload files to google cloud"
var mime = require('mime-types');
var path = require('path');
var {Storage} = require("@google-cloud/storage");
var importer = require('../Core');
var createBucket = importer.import("create a bucket");
var streamToOutput = importer.import("test stream to output");
var fetchOrStream = importer.import("fetch file or stream");
var storage = new Storage({
projectId: process.env.GOOGLE_STORAGE_PROJECT,
keyFilename: process.env.GOOGLE_STORAGE_CREDENTIALS
});
function streamToGoogle(fileUrl, bucketName, stream, metadata) {
var gcsname = (stream ? fileUrl : path.basename(fileUrl)).replace(/\?.*/ig, '');
console.log('project', storage.projectId, process.env.GOOGLE_STORAGE_PROJECT)
return createBucket(storage.projectId, bucketName)
.then(() => storage.bucket(bucketName).file(gcsname)
.createWriteStream({
metadata: Object.assign({
contentType: mime.lookup(gcsname)
}, metadata || {})
}))
.then(writeStream => fetchOrStream(stream || fileUrl, writeStream))
.then(() => `https://storage.googleapis.com/${bucketName}/${gcsname}`)
}
module.exports = streamToGoogle;
const mime = require('mime-types');
const path = require('path');
const { Storage } = require('@google-cloud/storage');
const importer = require('../Core');
const { createBucket, streamToOutput, fetchOrStream } = importer;
const storageConfig = {
projectId: process.env.GOOGLE_STORAGE_PROJECT,
keyFilename: process.env.GOOGLE_STORAGE_CREDENTIALS
};
class GoogleStorage {
constructor(storageConfig) {
this.storage = new Storage(storageConfig);
}
async createBucket(bucketName) {
try {
await createBucket(this.storage.projectId, bucketName);
return this.storage.bucket(bucketName);
} catch (error) {
console.error(`Error creating bucket ${bucketName}:`, error);
throw error;
}
}
async streamToGoogle(fileUrl, bucketName, stream, metadata) {
const gcsname = (stream? fileUrl : path.basename(fileUrl)).replace(/\?.*/ig, '');
const bucket = await this.createBucket(bucketName);
const file = bucket.file(gcsname);
const writeStream = file.createWriteStream({
metadata: Object.assign({
contentType: mime.lookup(gcsname)
}, metadata || {})
});
await fetchOrStream(stream || fileUrl, writeStream);
const url = `https://storage.googleapis.com/${bucketName}/${gcsname}`;
await streamToOutput(writeStream);
return url;
}
}
const googleStorage = new GoogleStorage(storageConfig);
module.exports = { googleStorage, streamToGoogle: googleStorage.streamToGoogle };
This code defines a utility function called streamToGoogle
that uploads a file or stream to a Google Cloud Storage bucket.
Here's a breakdown:
Dependencies:
mime
: Node.js module for determining MIME types based on file extensions.path
: Node.js module for working with file and directory paths.@google-cloud/storage
: Google Cloud Storage client library for Node.js.importer
: A custom module (likely located in ../Core
) used to import other functions.Imported Functions:
createBucket
: Function to create a Google Cloud Storage bucket.streamToOutput
: Function to download a file or stream and save it to a local file (likely used internally).fetchOrStream
: Function to download a file from a URL or process a stream (likely used internally).Google Cloud Storage Setup:
Storage
client using the project ID and credentials from environment variables.streamToGoogle
Function:
fileUrl
: The URL of the file to upload or a stream object.bucketName
: The name of the Google Cloud Storage bucket.stream
: An optional stream object containing the file data.metadata
: Optional metadata to associate with the uploaded file.gcsname
) based on the input.createBucket
.storage.bucket(bucketName).file(gcsname).createWriteStream
.mime.lookup
.fetchOrStream
to download the file or process the stream and write it to the write stream.Export:
module.exports = streamToGoogle;
: Makes the streamToGoogle
function available for use in other modules.In essence, this code provides a reusable function for uploading files or streams to Google Cloud Storage, handling bucket creation and metadata setting.